package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingStudyRecord;
import com.ruoyi.system.domain.TrainingStudyRecordCount;
import com.ruoyi.system.mapper.TrainingStudyRecordMapper;
import com.ruoyi.system.service.ITrainingStudyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 培训学习记录Service业务层处理
 *
 * @author dp
 * @date 2024-01-11
 */
@Service
public class TrainingStudyRecordServiceImpl implements ITrainingStudyRecordService {
    @Autowired
    private TrainingStudyRecordMapper trainingStudyRecordMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询培训学习记录
     *
     * @param studyRecordId 培训学习记录主键
     * @return 培训学习记录
     */
    @Override
    public TrainingStudyRecord selectTrainingStudyRecordByStudyRecordId(Long studyRecordId) {
        return trainingStudyRecordMapper.selectTrainingStudyRecordByStudyRecordId(studyRecordId);
    }

    /**
     * 查询培训学习记录列表
     *
     * @param trainingStudyRecord 培训学习记录
     * @return 培训学习记录
     */
    @Override
    public List<TrainingStudyRecord> selectTrainingStudyRecordList(TrainingStudyRecord trainingStudyRecord) {
        return trainingStudyRecordMapper.selectTrainingStudyRecordList(trainingStudyRecord);
    }

    /**
     * 查询培训学习记录合计列表
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param notesStatus
     * @param cadetId
     * @param cadetName
     * @return
     */
    @Override
    public List<TrainingStudyRecordCount> selectTrainingStudyRecordCount(
            String submitBegin, String submitEnd, String baseId, String notesStatus, Long cadetId, String cadetName) {
        return trainingStudyRecordMapper.selectTrainingStudyRecordCount(
                submitBegin, submitEnd, baseId, notesStatus, cadetId, cadetName);
    }

    @Override
    public List<TrainingStudyRecord> getRecordList(String submitBegin, String submitEnd, String baseId, String cadetId, String notesStatus) {
        return trainingStudyRecordMapper.getRecordList(submitBegin, submitEnd, baseId, cadetId, notesStatus);
    }

    /**
     * 新增培训学习记录
     *
     * @param trainingStudyRecord 培训学习记录
     * @return 结果
     */
    @Override
    public int insertTrainingStudyRecord(TrainingStudyRecord trainingStudyRecord) {
        trainingStudyRecord.setCreateTime(DateUtils.getNowDate());
        if (trainingStudyRecord.getMentorId() != null && trainingStudyRecord.getMentorId().isEmpty()) {
            trainingStudyRecord.setMentorId(null);
        }
        return trainingStudyRecordMapper.insertTrainingStudyRecord(trainingStudyRecord);
    }

    /**
     * 修改培训学习记录
     *
     * @param trainingStudyRecord 培训学习记录
     * @return 结果
     */
    @Override
    public int updateTrainingStudyRecord(TrainingStudyRecord trainingStudyRecord) {
        if (trainingStudyRecord.getMentorId() != null && trainingStudyRecord.getMentorId().isEmpty()) {
            trainingStudyRecord.setMentorId(null);
        }
        return trainingStudyRecordMapper.updateTrainingStudyRecord(trainingStudyRecord);
    }

    /**
     * 批量删除培训学习记录
     *
     * @param studyRecordIds 需要删除的培训学习记录主键
     * @return 结果
     */
    @Override
    public int deleteTrainingStudyRecordByStudyRecordIds(Long[] studyRecordIds) {
        return trainingStudyRecordMapper.deleteTrainingStudyRecordByStudyRecordIds(studyRecordIds);
    }

    /**
     * 删除培训学习记录信息
     *
     * @param studyRecordId 培训学习记录主键
     * @return 结果
     */
    @Override
    public int deleteTrainingStudyRecordByStudyRecordId(Long studyRecordId) {
        return trainingStudyRecordMapper.deleteTrainingStudyRecordByStudyRecordId(studyRecordId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingStudyRecord 实体
     * @param userId              用户id
     * @param crateType           创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingStudyRecord trainingStudyRecord, Long userId, Integer crateType) {
        //主键
        if (trainingStudyRecord.getStudyRecordId() == null || "".equals(trainingStudyRecord.getStudyRecordId())) {
            trainingStudyRecord.setStudyRecordId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingStudyRecord.getCreateBy() == null || "".equals(trainingStudyRecord.getCreateBy())) {
                trainingStudyRecord.setCreateBy(userId.toString());
            }
            if (trainingStudyRecord.getCreateTime() == null) {
                trainingStudyRecord.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingStudyRecord.getModifyBy() == null) {
            trainingStudyRecord.setModifyBy(userId.toString());
        }
        //修改时间
        if (trainingStudyRecord.getModifyTime() == null) {
            trainingStudyRecord.setModifyTime(DateUtils.getNowDate());
        }
    }

}
